package com.jx.lcs.employee.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jx.lcs.employee.dao.EmployeeDao;
import com.jx.lcs.employee.entity.Employee;
import com.jx.lcs.user.dao.UserInfoDao;
import com.jx.lcs.user.entity.UserInfo;
import com.tianyu.jty.common.persistence.HibernateDao;
import com.tianyu.jty.common.service.BaseService;

/** 
 * 理财师Service
 * @author JiaYY 
 * @date 2015年7月2日
 */
@Service
@Transactional(readOnly=true)
public class EmployeeService extends BaseService<Employee, String> {

	@Autowired
	private EmployeeDao employeeDao;
	
	@Autowired
	private UserInfoDao userInfoDao;

	@Override
	public HibernateDao<Employee, String> getEntityDao() {
		return employeeDao;
	}
	
	/**
	 * 根据手机号查询理财师
	 * @param telephone
	 * @return
	 */
	public Employee findByTelephone(String telephone) {
		List<Employee> list = employeeDao.findBy("telephone", telephone);
		Employee employee = null;
		if(list != null && list.size() > 0) {
			employee = list.get(0);
		}
		return employee;
	}
	
	/**
	 * 根据员工号查询理财师
	 * @param userCode
	 * @return
	 */
	public Employee findByUserCode(String userCode) {
		List<Employee> list = employeeDao.findBy("userCode", userCode);
		Employee employee = null;
		if(list != null && list.size() > 0) {
			employee = list.get(0);
		}
		return employee;
	}
	
	/**
	 * 根据邀请码查询理财师
	 * @param jxId
	 * @return
	 */
	public Employee findByJxId(String jxId) {
		List<Employee> list = employeeDao.findBy("jxId", jxId);
		Employee employee = null;
		if(list != null && list.size() > 0) {
			employee = list.get(0);
		}
		return employee;
	}
	
	/**
	 * 根据理财师姓名获取理财师信息
	 * @param userName
	 * @return
	 */
	public Employee getEmployeeByuserName(String userName) {
		List<Employee> emList = employeeDao.findBy("userName", userName);
		Employee employee = null;
		if(emList != null && emList.size() > 0) {
			employee = emList.get(0);
		}
		return employee;
	}
	
	/**
	 * 修改理财师信息
	 * @return
	 */
	@Transactional(readOnly = false)
	public void updateEmployee(Employee employee, UserInfo userInfo) throws Exception {
		employeeDao.save(employee);
		if(userInfo != null) {
			userInfoDao.save(userInfo);
		}
	}
}
